package vip.lsjscl.flowboot.workflow.service;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.lsjscl.flowboot.common.BusinessException;
import vip.lsjscl.flowboot.workflow.entity.DKActivity;
import vip.lsjscl.flowboot.workflow.entity.DKWorkflow;
import vip.lsjscl.flowboot.workflow.entity.DKWorkflowVersion;
import vip.lsjscl.flowboot.workflow.repository.DKActivityRepository;
import vip.lsjscl.flowboot.workflow.repository.DKWorkflowRepository;
import vip.lsjscl.flowboot.workflow.repository.DKWorkflowVersionRepository;

import java.util.List;

/**
 * 工作流版本
 *
 * @author zhangxingju
 * @date 2025/02/14
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class DKWorkflowVersionService {

    private final DKWorkflowRepository workflowRepository;
    private final DKWorkflowVersionRepository workflowVersionRepository;
    private final DKActivityRepository activityRepository;

    /**
     * 获取最新版本的工作流
     */
    public DKWorkflowVersion getLatestWorkflowVersion(String workflowCode) {
        DKWorkflow workflow = workflowRepository.findByCode(workflowCode).orElseThrow(() -> new BusinessException("未找到对应的工作流定义"));

        return workflowVersionRepository
                .findByWorkflowIdOrderByVersionDesc(workflow.getId())
                .stream()
                .findFirst()
                .orElseThrow(() -> new BusinessException("工作流未发布，请先发布工作流"));
    }

    public List<DKWorkflow> findAll() {
        List<DKWorkflow> workflows = workflowRepository.findAll();
        for (DKWorkflow workflow : workflows) {
            Integer currentVersion = workflowVersionRepository.findMaxVersionByWorkflowId(workflow.getId());
            workflow.setCurrentVersion(currentVersion);
            List<DKWorkflowVersion> versions = workflowVersionRepository.findByWorkflowIdOrderByVersionDesc(workflow.getId());
            workflow.setVersions(versions);
        }
        return workflows;
    }

    public List<DKWorkflowVersion> getWorkflowVersions(String workflowId) {
        return workflowVersionRepository.findByWorkflowIdOrderByVersionDesc(workflowId);
    }

    public DKWorkflowVersion getWorkflowVersionByBusinessId(String businessId) {
        DKActivity activity = activityRepository.findFirstByBusinessId(businessId);
        return workflowVersionRepository.findById(activity.getWorkflowVersionId()).orElseThrow(() -> new BusinessException("工作流版本不存在"));
    }

    public DKWorkflowVersion publishWorkflow(String id) {
        // 查找对应的工作流定义
        DKWorkflow workflow = workflowRepository.findById(id).orElseThrow(() -> new BusinessException("工作流不存在"));

        // 确定新的版本号
        List<DKWorkflowVersion> versions = workflowVersionRepository.findByWorkflowIdOrderByVersionDesc(id);
        int newVersion = versions.isEmpty() ? 1 : versions.get(0).getVersion() + 1;

        // 创建新的工作流版本记录
        DKWorkflowVersion workflowVersion = new DKWorkflowVersion();
        workflowVersion.setWorkflowId(id);
        workflowVersion.setName(workflow.getName());
        workflowVersion.setCode(workflow.getCode());
        workflowVersion.setVersion(newVersion);
        workflowVersion.setFlowData(workflow.getFlowData());
        workflowVersion = workflowVersionRepository.save(workflowVersion);

        // 更新工作流状态为已发布
        workflow.setStatus(1);
        workflowRepository.save(workflow);

        return workflowVersion;
    }

    public DKWorkflowVersion findById(String versionId) {
        return workflowVersionRepository.findById(versionId).orElseThrow(() -> new BusinessException("工作流版本不存在"));
    }
}